<template>
  <div>
    <div style="margin: 10px 0">
      <el-input style="width: 200px" placeholder="请输入名称" suffix-icon="el-icon-search" v-model="name"></el-input>
      <el-button class="ml-5" type="primary" @click="onLoad">搜索</el-button>
      <el-button type="warning" @click="reset">重置</el-button>
    </div>

    <el-table :data="tableData" border stripe :header-cell-class-name="'headerBg'"  @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"></el-table-column>
      <el-table-column prop="id" label="编号" width="120"></el-table-column>
      <el-table-column prop="coverImg" label="商品图片" width="120" align="center">
        <template slot-scope="scope" >
          <img :src="scope.row.coverImg" alt="" style="height: 80px;"> 
        </template>
      </el-table-column>
      <el-table-column prop="name" label="商品名称" align="center"></el-table-column>
      <el-table-column prop="description" label="价格" align="center"></el-table-column>
      <el-table-column prop="description" label="标签" align="center">
        <template slot-scope="scope">
          <div >
            <p>
              上架: 
              <el-switch
              @change="updateSaleable(scope.row)"
                v-model="scope.row.saleable">
              </el-switch>
            </p>
            
            <p>
              推荐: 
              <el-switch
                @change="updateRecommend(scope.row)"
                v-model="scope.row.recommend">
              </el-switch>
            </p>
          </div>
        </template>
        
      </el-table-column>
      <el-table-column prop="description" label="sku库存" align="center"></el-table-column>
      <el-table-column label="操作"  width="280" align="center">
        <template slot-scope="scope">
          <el-button type="success" @click="handleEdit(scope.row)">编辑 <i class="el-icon-edit"></i></el-button>
          <el-popconfirm
              class="ml-5"
              confirm-button-text='确定'
              cancel-button-text='我再想想'
              icon="el-icon-info"
              icon-color="red"
              title="您确定删除吗？"
              @onConfirm="del(scope.row.id)"
          >
            <el-button type="danger" slot="reference">删除 <i class="el-icon-remove-outline"></i></el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div style="padding: 10px 0">
      <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          :page-sizes="[2, 5, 10, 20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </div>
  </div>
</template>

<script>

import { getProductList, saveProduct, deleteProduct } from '@/api/product'

export default {
  name: "Product",
  data() {
    return {
      tableData: [],
      total: 0,
      pageNum: 1,
      pageSize: 10,
      name: "",
      form: {},
      props: {
        label: 'name',
      },
      expends: [],
      checks: [],
      roleId: 0,
      roleFlag: '',
      ids: []
    }
  },
  created() {
    this.onLoad()
  },
  methods: {
    onLoad() {
      getProductList({
          pageNum: this.pageNum,
          pageSize: this.pageSize,
          name: this.name,
        }).then(res => {
          this.tableData = res.data.records
          this.total = res.data.total
      })
    },
    updateSaleable(row) {
      console.log(!row.saleable);
      saveProduct({
        id: row.id,
        saleable: row.saleable
      }).then(res => {
        this.$message.success("更新成功")
      })
    },
    updateRecommend(row) {
      saveProduct({
        id: row.id,
        recommend: row.recommend
      }).then(res => {
        this.$message.success("更新成功")
      })
    },
    handleAdd() {
      this.dialogFormVisible = true
      this.form = {}
    },
    handleEdit(row) {
      this.$router.push({
        path: '/pms/updateProduct',
        query: {
          id: row.id
        }
      })
    },
    del(id) {
      console.log(6);
      deleteProduct({id: id}).then(res => {
        if (res.code === 200) {
          this.$message.success("删除成功")
          this.onLoad()
        } else {
          this.$message.error("删除失败")
        }
      })
    },
    handleSelectionChange(val) {
      console.log(val)
      this.multipleSelection = val
    },
    delBatch() {
      let ids = this.multipleSelection.map(v => v.id)  // [{}, {}, {}] => [1,2,3]
      this.request.post("/role/del/batch", ids).then(res => {
        if (res.code === '200') {
          this.$message.success("批量删除成功")
          this.onLoad()
        } else {
          this.$message.error("批量删除失败")
        }
      })
    },
    reset() {
      this.name = ""
      this.onLoad()
    },
    handleSizeChange(pageSize) {
      this.pageSize = pageSize
      this.onLoad()
    },
    handleCurrentChange(pageNum) {
      this.pageNum = pageNum
      this.onLoad()
    },
  }
}
</script>


<style>
.headerBg {
  background: #eee!important;
}
</style>